package com.hxy.modules.demo.controller;



import com.hxy.modules.common.page.Page;
import com.hxy.modules.common.utils.StringUtils;
import com.hxy.modules.common.utils.Utils;
import com.hxy.modules.demo.entity.LeaveEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import com.hxy.modules.demo.entity.BudgetaplyEntity;
import com.hxy.modules.demo.service.BudgetaplyService;
import com.hxy.modules.common.utils.Result;

import javax.servlet.http.HttpServletRequest;


/**
 * 预算申请
 * 
 * @author hxy
 * @email huangxianyuan@gmail.com
 * @date 2018-05-11 17:17:14
 */
@Controller
@RequestMapping("demo/budget")
public class BudgetaplyController {
	@Autowired
	private BudgetaplyService budgetaplyService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("act:model:all")
	public String list(Model model , BudgetaplyEntity budgetaplyEntity, HttpServletRequest request){
		int pageNum = Utils.parseInt(request.getParameter("pageNum"), 1);
		Page<BudgetaplyEntity> page = budgetaplyService.findPage(budgetaplyEntity, pageNum);
		model.addAttribute("page",page);
		model.addAttribute("leave",budgetaplyEntity);
		return "demo/budget";
	}

	/**
	 * 预算详情
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("info")
	@RequiresPermissions("act:model:all")
	public String info(Model model , String id, HttpServletRequest request){
		if(!StringUtils.isEmpty(id)){
			BudgetaplyEntity budgetaplyEntity = budgetaplyService.queryObject(id);
			model.addAttribute("budget",budgetaplyEntity);
		}
		return "demo/budgetEdit";
	}
	
	/**
	 * 保存
	 */
	@RequestMapping(value = "edit",method = RequestMethod.POST)
	@RequiresPermissions("act:model:all")
	@ResponseBody
	public Result edit(BudgetaplyEntity budget){

		if(StringUtils.isEmpty(budget.getId())){

			budgetaplyService.save(budget);

		} else {

			budgetaplyService.update(budget);

		}

		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("act:model:all")
	public Result delete(@RequestBody String[] ids){
		budgetaplyService.deleteBatch(ids);
		
		return Result.ok();
	}
	
}
